<?php

/**
 * This is the model class for table "tbl_coa".
 *
 * The followings are the available columns in table 'tbl_coa':
 * @property string $account
 * @property string $id
 * @property string $desc1
 * @property string $dept
 * @property string $div
 * @property string $group
 * @property string $type
 * @property integer $isdept
 * @property string $accountsort
 * @property integer $inactive
 * @property string $accrualvendor
 * @property string $recondate
 * @property double $reconbeginbal
 * @property double $reconendbal
 * @property integer $overheadmethod
 * @property integer $wipaccount
 * @property string $wipclosingaccountid
 * @property string $parentid
 */
class Coa extends CanLockRecord
{
        const INCREASE=0;
       const DECREASE=1;
       const DEBIT=0;
       const CREDIT=1;
  /**
   * Returns the static model of the specified AR class.
   * @return Coa the static model class
   */
  public static function model($className=__CLASS__)
  {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName()
  {
    return 'tbl_coa';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules()
  {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
      array('account, id, desc1, isdept,  accountsort, inactive, accrualvendor, reconbeginbal, reconendbal, overheadmethod, wipaccount', 'required'),
      array('isdept, inactive, overheadmethod, wipaccount', 'numerical', 'integerOnly'=>
true),
      array('reconbeginbal, reconendbal', 'numerical'),
      array('account, accountsort', 'length', 'max'=>10),
      array('id, dept, div', 'length', 'max'=>2),
      array('desc1', 'length', 'max'=>50),
      array('group, accrualvendor', 'length', 'max'=>4),
      array('type', 'length', 'max'=>1),      
      array('wipclosingaccountid, parentid', 'length', 'max'=>36),
      array('recondate', 'safe'),
      // The following rule is used by search().
      // Please remove those attributes that should not be searched.
      array('account, id, desc1, dept, div, group, type, isdept,  accountsort, inactive, accrualvendor, recondate, reconbeginbal, reconendbal, overheadmethod, wipaccount, wipclosingaccountid, parentid', 'safe', 'on'=>'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations()
  {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels()
  {
    return array(
      'account' => 'Account',
      'id' => 'ID',
      'desc1' => 'Desc1',
      'dept' => 'Dept',
      'div' => 'Div',
      'group' => 'Group',
      'type' => 'Type',
      'isdept' => 'Isdept',     
      'accountsort' => 'Accountsort',
      'inactive' => 'Inactive',
      'accrualvendor' => 'Accrualvendor',
      'recondate' => 'Recondate',
      'reconbeginbal' => 'Reconbeginbal',
      'reconendbal' => 'Reconendbal',
      'overheadmethod' => 'Overheadmethod',
      'wipaccount' => 'Wipaccount',
      'wipclosingaccountid' => 'Wipclosingaccountid',
      'parentid' => 'Parentid',
    );
  }

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search()
  {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria=new CDbCriteria;

    $criteria->compare('account',$this->account,true);
    $criteria->compare('id',$this->id,true);
    $criteria->compare('desc1',$this->desc1,true);
    $criteria->compare('dept',$this->dept,true);
    $criteria->compare('div',$this->div,true);
    $criteria->compare('group',$this->group,true);
    $criteria->compare('type',$this->type,true);
    $criteria->compare('isdept',$this->isdept);
    
    $criteria->compare('accountsort',$this->accountsort,true);
    $criteria->compare('inactive',$this->inactive);
    $criteria->compare('accrualvendor',$this->accrualvendor,true);
    $criteria->compare('recondate',$this->recondate,true);
    $criteria->compare('reconbeginbal',$this->reconbeginbal);
    $criteria->compare('reconendbal',$this->reconendbal);
    $criteria->compare('overheadmethod',$this->overheadmethod);
    $criteria->compare('wipaccount',$this->wipaccount);
    $criteria->compare('wipclosingaccountid',$this->wipclosingaccountid,true);
    $criteria->compare('parentid',$this->parentid,true);

    return new CActiveDataProvider($this, array(
      'criteria'=>$criteria,
    ));
  }
        public function init() {
            parent::init();
            $this->isdept=0;
            $this->inactive=0;
            $this->reconbeginbal=0;
            $this->reconendbal=0;
            $this->overheadmethod=0;
            $this->wipaccount=0;
            $this->accrualvendor=NULL;
            $this->wipclosingaccountid=NULL;
            $this->dept=NULL;
            $this->div=NULL;
            $this->group=NULL;
            $this->type="S";
            $this->recondate=NULL;
        }
//        
//         public function getAssignedWareHouse(){
//            $data=Yii::app()->db->createCommand('select * from tbl_warehouse where empno=:empno AND status=1')->bindValue('empno', $this->empno)->queryAll();
//            $rs=array();
//            if(count($data)>0){
//                foreach($data as $item){
//                    $rs[]=array(
//                        'warehouseid'=>$item['warehouseid'],
//                        'warehousedesc'=>$item['warehousedesc']
//                    );
//                }
//            }
//            return $rs;
//        }
        
//       public function getCOATypeText(){
//         return isset($this->COAList[$this->group])?$this->COAList[$this->group]:NULL;
//    }
       public function getListWithDesc(){
           $data=Yii::app()->db->createCommand('select * from tbl_coa where inactive=0' )->queryAll();
            $rs=array(
              
            );
            if(count($data)>0){
                foreach($data as $item){
                    $rs[$item['account']]=$item['account'].' - '.$item['desc1'] ;                       
                    
                }
            }
            return $rs;
       }
       public function getCOAList(){
          // $data=Yii::app()->db->createCommand('select * from tbl_coa where id!=:id' )->bindValue('id',$this->id)->queryAll();
          $data=Yii::app()->db->createCommand('select * from tbl_coa' )->queryAll();
            $rs=array(
                ''=>'N/A'
            );
            if(count($data)>0){
                foreach($data as $item){
                  $space = "";
                  if(strlen(trim($item['parentid'])>0))
                  {
                      $space = "---";
                  }
                  $rs[$item['account']]= $space.$item['account']." - ".$item['desc1'] ;                       
                }
            }
            return $rs;
       }   
       public function getList(){
           $coalist=$this->findAll('inactive=0');
           $rs=array();
           foreach($coalist as $item){
               $rs[$item->account]=$item->account;
           }
           return $rs;
       }
       public function getCategoryList(){
           $data=Acctcat::model()->findAll();
           $rs=array();
           if(count($data)>0){
               foreach($data as $item){
                   $rs[$item->id]=$item->desc1;
               }
           }
           return $rs;
       }
       public function getCategoryText(){
          $acc=Acctcat::model()->find('id=:id',array('id'=>$this->id));
          return $acc->desc1;
       }
       public function getInactiveList(){
           return array(
               '-1'=>'Inactive',
               '0'=>'Active'
           );
       }
       public function getInactiveText(){
           return isset($this->inactiveList[$this->inactive])?$this->inactiveList[$this->inactive]:NULL;
       }
       public function getAccountName($id=NULL){
           if($id==NULL) $id=$this->account;
           $name=Yii::app()->db->createCommand('select desc1 from tbl_coa where account=:accountid')->bindValue('accountid', $id)->queryScalar();
           if($name!==false){
               return $name;
           }
           return NULL;
       }
       public function getAccountIncreasesDecreases($account){
           $rs=array();
           $name=Yii::app()->db->createCommand('select cat.group from tbl_acctcat cat 
               left join tbl_coa coa on coa.id = cat.id
               where coa.account=:account')->bindValue('account', $account)->queryScalar();
           if($name!==false){
               switch($name){
                   case 'L':
                       $rs[self::CREDIT]=self::INCREASE;
                       $rs[self::DEBIT]=self::DECREASE;
                       break;
                   case 'A':
                       $rs[self::CREDIT]=self::DECREASE;
                       $rs[self::DEBIT]=self::INCREASE;
                       break;
                   case 'I':
                       $rs[self::CREDIT]=self::INCREASE;
                       $rs[self::DEBIT]=self::DECREASE;
                       break;
                   case 'E':
                       $rs[self::CREDIT]=self::DECREASE;
                       $rs[self::DEBIT]=self::INCREASE;
                       break;
               }
               return $rs;
           }
       }
       
       public function getAccountType(){
           
       }
       
}